package com.loippi.core.gen.processor;

import com.loippi.core.gen.utils.StringUtils;
import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.*;
import java.util.Map;

/**
 * @version V1.0
 * @Description: 覆盖代码生成器方法
 * @author: wangcj
 * @date: 2018年8月21日 上午10:08:41
 */
public class Generator {
    private static void generate(Map<String, Object> data, String path, String fileName, String templateName) {
        try {
            Configuration cfg = FreemarkerConfig.getConfiguration();
//            Template template = cfg.getTemplate(templateName);
            freemarker.template.Template template = cfg.getTemplate(templateName);
            File file = new File(path + fileName);
            File directory = file.getParentFile();
            if (!(directory.exists())) {
                directory.mkdirs();
            }
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
            template.process(data, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String generate(Map<String, Object> data, String templateName) {
        try {
            Configuration cfg = FreemarkerConfig.getConfiguration();
            Template template = cfg.getTemplate(templateName);
            StringWriter out = new StringWriter();
            template.process(data, out);
            return out.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static void initGenerateConfig(Map<String, Object> data, String className) {
        data.put("classNamePath", StringUtils.camelToUnderline(className));
        data.put("GenericDao", "com.framework.loippi.mybatis.dao.GenericDao");
        data.put("GenericEntity", "com.framework.loippi.mybatis.eitity.GenericEntity");
        data.put("GenericService", "com.framework.loippi.service.GenericService");
        data.put("GenericServiceImpl", "com.framework.loippi.service.impl.GenericServiceImpl");
        data.put("GenericController", "com.framework.loippi.controller.admin.GenericController");
        data.put("current_version", "2.0");
    }

    public static String runPostman(Map<String, Object> data) {
        return generate(data, "postman.ftl");
    }

    public static void runDTO(String path, String packageName, Map<String, Object> data, String functionName) {
        data.put("packageName", packageName.toLowerCase());
        generate(data, path + "\\" + packageName + "\\com\\framework\\loippi\\controller\\api\\params\\" + packageName.toLowerCase() + "\\", functionName.concat("Param.java"), "params.ftl");
        generate(data, path + "\\" + packageName + "\\com\\framework\\loippi\\controller\\api\\result\\" + packageName.toLowerCase() + "\\", functionName.concat("Result.java"), "results.ftl");
    }

    public static void runAPI(String path, String packageName, Map<String, Object> data) {
        generate(data, path + "\\" + packageName + "\\com\\framework\\loippi\\controller\\api\\", StringUtils.upperFirst(packageName).concat("APIController.java"), "api.ftl");
    }

    public static void runEntity(Map<String, Object> data, String path, String className, String packageName) {
        initGenerateConfig(data, className);
        generate(data, path + "\\" + className.toString().toLowerCase() + "\\" + packageName.replace(".", "\\") + "\\entity\\", className.concat(".java"), "entity.ftl");
    }

    public static void runDao(Map<String, Object> data, String path, String className, String packageName) {
        initGenerateConfig(data, className);
        generate(data, path + "\\" + className.toString().toLowerCase() + "\\" + packageName.replace(".", "\\") + "\\dao\\", className.concat("Dao.java"), "dao.ftl");
    }

    public static void runMapper(Map<String, Object> data, String path, String className, String packageName) {
        initGenerateConfig(data, className);
        generate(data, path + "\\" + className.toString().toLowerCase() + "\\" + packageName.replace(".", "\\") + "\\mappers\\", className.concat("Dao.xml"), "mapper.ftl");
    }

    public static void runService(Map<String, Object> data, String path, String className, String packageName) {
        initGenerateConfig(data, className);
        generate(data, path + "\\" + className.toString().toLowerCase() + "\\" + packageName.replace(".", "\\") + "\\service\\", className.concat("Service.java"), "service.ftl");
    }

    public static void runServiceImpl(Map<String, Object> data, String path, String className, String packageName) {
        initGenerateConfig(data, className);
        generate(data, path + "\\" + className.toString().toLowerCase() + "\\" + packageName.replace(".", "\\") + "\\service\\impl\\", className.concat("ServiceImpl.java"), "service_impl.ftl");
    }

    public static void runController(Map<String, Object> data, String path, String className, String packageName) {
        initGenerateConfig(data, className);
        generate(data, path + "\\" + className.toString().toLowerCase() + "\\" + packageName.replace(".", "\\") + "\\controller\\admin\\", className.concat("Controller.java"), "controller.ftl");
    }

    public static void runAPIController(Map<String, Object> data, String path, String className, String packageName) {
        initGenerateConfig(data, className);
        generate(data, path + "\\" + className.toString().toLowerCase() + "\\" + packageName.replace(".", "\\") + "\\controller\\api\\", className.concat("APIController.java"), "apicontroller.ftl");
    }

    public static void runView(Map<String, Object> data, String path, String className, String packageName) {
        initGenerateConfig(data, className);
        generate(data, path + "\\" + className.toString().toLowerCase() + "\\template\\admin\\" + StringUtils.camelToUnderline(className) + "\\", "add.ftl", "add.ftl");
        generate(data, path + "\\" + className.toString().toLowerCase() + "\\template\\admin\\" + StringUtils.camelToUnderline(className) + "\\", "edit.ftl", "edit.ftl");
        generate(data, path + "\\" + className.toString().toLowerCase() + "\\template\\admin\\" + StringUtils.camelToUnderline(className) + "\\", "view.ftl", "view.ftl");
        generate(data, path + "\\" + className.toString().toLowerCase() + "\\template\\admin\\" + StringUtils.camelToUnderline(className) + "\\", "list.ftl", "list.ftl");
    }
}